package com.tianji.learning.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.exceptions.BadRequestException;
import com.tianji.common.utils.BeanUtils;
import com.tianji.learning.dto.QuestionFormDTO;
import com.tianji.learning.dto.QuestionUpdateDTO;
import com.tianji.learning.entity.InteractionQuestion;
import com.tianji.learning.query.QuestionPageQuery;
import com.tianji.learning.service.IInteractionQuestionService;
import com.tianji.learning.vo.QuestionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 互动提问的问题表 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-05-29
 */
@RestController
@RequestMapping("/questions")
@RequiredArgsConstructor
@Api("用户端：互动提问的问题表")
@Slf4j
public class InteractionQuestionController {

    private final IInteractionQuestionService questionService;

    @ApiOperation("根据id查询问题详情")
    @GetMapping("/{id}")
    public QuestionVO getInfo(@PathVariable("id") Long id){
        return questionService.getInfo(id);
    }

    @ApiOperation("分页查询互动问题")
    @GetMapping("page")
    public PageDTO<QuestionVO> queryQuestionPage(QuestionPageQuery query){
        return questionService.queryQuestionPage(query);
    }

    @ApiOperation("新增提问")
    @PostMapping
    public void saveQuestion(@Valid @RequestBody QuestionFormDTO questionDTO){
        log.info("新增提问：{}",questionDTO);
        questionService.saveQuestion(questionDTO);
    }

    @ApiOperation("修改提问")
    @PutMapping("/{id}")
    public void updateQuestion(@RequestBody QuestionUpdateDTO dto, @PathVariable("id") Long id){
        if (id == null) {
            throw new BadRequestException("id不能为空");
        }
        log.info("修改提问：{}",dto);
        dto.setId(id);
        InteractionQuestion po = BeanUtils.copyBean(dto, InteractionQuestion.class);
        questionService.updateById(po);
    }

    @ApiOperation("删除提问")
    @DeleteMapping("/{id}")
    public void deleteQuestion(@PathVariable("id") Long id){
        if (id == null) {
            throw new BadRequestException("id不能为空");
        }
        log.info("删除提问：{}",id);
        questionService.removeById(id);
    }

}
